package com.learn.sunday.stock.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.sunday.stock.bean.Goods;
import com.learn.sunday.stock.mapper.GoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Slf4j
@RestController
@RequestMapping
public class GoodsController {

    @Autowired
    private GoodsMapper goodsMapper;
    Random random = new Random();
    @GetMapping("save")
    public String saveTest(){

        Goods goods = new Goods();
        goods.setGstatus("status" + random.nextInt(200));
        goods.setUserId(1 + random.nextInt(50000));
        goods.setGname("name" + random.nextInt(10));
        int insert = goodsMapper.insert(goods);
        log.info("插入结果是:{}",insert);
        return "myqxin";
    }

    @GetMapping("list")
    public List<Goods> list(){
        log.info("hello apm1");
        Map<String, Object> map = new HashMap<>();
        return goodsMapper.selectByMap(map);
    }

    @GetMapping("find")
    public Goods listOne(@Param("id")Long id){
        log.info("hello apm2");
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gid",id);
        return goodsMapper.selectOne(queryWrapper);
    }
}
